/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-5-11 下午2:37 Created by dwzhang
 */
package com.jade.bss.ec.order.valve;

import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.base.BssException;

import com.jade.bss.base.customer.CustomerManager;
import com.jade.bss.ec.order.OrderContext;
import com.jade.bss.ec.order.OrderException;
import com.jade.bss.ec.order.OrderManager;
import com.jade.bss.ec.ware.WareManager;
import com.jade.framework.base.pattern.pipeline.Valve;
import com.jade.framework.base.pattern.pipeline.ValveChain;
import org.springframework.transaction.annotation.Transactional;

/**
 * 抽象的订单处理阀门。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-5-11 下午2:37
 */
public abstract class AbstractOrderValve
        extends Valve<OrderContext, OrderException>
{
    protected OrderManager orderManager;
    protected WareManager wareManager;
    protected CustomerManager customerManager;

    @Inject
    public void setWareManager(@Named ("bss_wareManager") WareManager wareManager)
    {
        this.wareManager = wareManager;
    }

    @Inject
    public void setOrderManager(@Named ("bss_orderManager") OrderManager orderManager)
    {
        this.orderManager = orderManager;
    }

    @Inject
    public void setCustomerManager(@Named ("bss_customerManager") CustomerManager customerManager)
    {
        this.customerManager = customerManager;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void handle(OrderContext orderContext, ValveChain<OrderContext, OrderException> chain)
            throws OrderException
    {
        doHandle(orderContext);
        chain.handleNext(orderContext);
    }

    protected abstract void doHandle(OrderContext context)
            throws OrderException;
}
